#pragma once 
#include "thread_pool.hpp"
#include "tcp_server.hpp"
#include <pthread.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unordered_map>
#include <string>
namespace ns_handler {
    using namespace ns_tcpserver;
    
    std::unordered_map<std::string,std::string> dict = {
        {"cat","猫"},
        {"dog","狗"}
    };

    #define SIZE 1024
    void HandlerHelper(int sock) {
        char buffer[1024] = {0};
        ssize_t s = read(sock, buffer, sizeof(buffer)-1);
        if (s > 0) {
            buffer[s] = 0;
            std::cout << "client say# " << buffer << std::endl;
            std::string k = buffer;
            std::string message = "我不知道 ";
            auto it = dict.find(k);
            if (it != dict.end()) {
                message = it->second;
            }
            write(sock, message.c_str(), message.size());
            std::cout << "server response# " << message  << std::endl;
        }

        //while (true) {
        //    char buffer[SIZE] = {0};
        //    ssize_t s = read(sock, buffer, sizeof(buffer)-1);
        //    if (s > 0) {
        //        // success
        //        buffer[s] = 0;
        //        std::cout << "client# " <<  buffer << std::endl;
        //        std::string echo_string = buffer;
        //        if (echo_string == "quit") {
        //            break;
        //        }
        //        echo_string += "[server say]";
        //        write(sock, echo_string.c_str(), echo_string.size());
        //    } else if (s == 0) {
        //        std::cout << "client quit.... " << std::endl;
        //        break;
        //    } else {
        //        std::cerr << "read error...." << std::endl;
        //        break;
        //    }
        //}
    }
    
    // 单进程
    void HandlerSock_V1(int sock) {
        HandlerHelper(sock);
    }

    // 多进程
    void HandlerSock_V2(int sock) {
        if (fork() == 0) {
            if (fork() > 0) {
                // 子进程退出
                exit(0);
            }
            // （子）父进程退出，孙子进程被OS领养
            HandlerHelper(sock);
            exit(0);
        }
        // 父进程回收子进程
        waitpid(-1, nullptr, 0);
    }

    void* thread_routine(void *args) {
        int sock = *((int *)args);
        delete (int*)args;
        pthread_detach(pthread_self());
        HandlerHelper(sock);
        // 已经处理完毕要关闭不需要的fd，不关闭会造成文件描述符泄露
        close(sock);
        return nullptr;
    }

    void HandlerSock_v3(int sock) {
        pthread_t tid;
        int *p = new int(sock);
        pthread_create(&tid, nullptr, thread_routine, p);
    }

    class task {
        public:
            int sock;
        public:
            task(){}
            task(int _sock)
            {
                sock = _sock;
            }
            void operator()() {
                HandlerHelper(sock);
                close(sock);
            }
            ~task(){}
    };

    // 线程池
    void HandlerSock_v4(int sock) {
        std::cout << "debug3-> " << sock << std::endl;
        task t(sock);
        // 这里要用static修饰，不然出了这个函数，栈上变量销毁了，而线程池里还在运行
        static ThreadPool<task> ins(5);
        ins.PushTask(t);
        //while (true);
    }
}
